package com.kanshu.ksgb.fastread.doudou.module.bookcity.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.dl7.recycler.helper.RecyclerViewHelper;
import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.app.constants.ARouterConfig;
import com.kanshu.ksgb.fastread.doudou.app.constants.Constants;
import com.kanshu.ksgb.fastread.doudou.base.basemvp.IGenrialMvpView;
import com.kanshu.ksgb.fastread.doudou.base.baseui.BaseFragment;
import com.kanshu.ksgb.fastread.doudou.common.business.ad.presenter.AdPresenter;
import com.kanshu.ksgb.fastread.doudou.common.business.interfaces.ISwitchTab;
import com.kanshu.ksgb.fastread.doudou.common.net.bean.BaseResult;
import com.kanshu.ksgb.fastread.doudou.common.util.ARouterUtils;
import com.kanshu.ksgb.fastread.doudou.common.util.DisplayUtils;
import com.kanshu.ksgb.fastread.doudou.common.util.NetUtils;
import com.kanshu.ksgb.fastread.doudou.common.util.SwipeRefreshHelper;
import com.kanshu.ksgb.fastread.doudou.common.util.Utils;
import com.kanshu.ksgb.fastread.doudou.common.view.EmptyLayout;
import com.kanshu.ksgb.fastread.doudou.common.view.TitlebarView;
import com.kanshu.ksgb.fastread.doudou.common.business.commonbean.BookInfo;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.adapter.TopicListAdapter;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.bean.SelectedBean;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.presenter.BookCityPresenter;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.retrofit.SelectedRequestParams;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.view.AdSelectedTopicHeaderLayout;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by wcy on 18/5/13
 */
@Route(path = ARouterConfig.HOME_PAGE_FIND)
public class FindFragment extends BaseFragment implements IGenrialMvpView<BaseResult<List<SelectedBean>>>, ISwitchTab {
    @BindView(R.id.title)
    TitlebarView mTitle;
    @BindView(R.id.recyler_view)
    RecyclerView mRecylerView;
    @BindView(R.id.empty_layout)
    EmptyLayout mEmptyLayout;
    @BindView(R.id.swipe_refresh)
    TwinklingRefreshLayout mSwipeRefresh;
    Unbinder unbinder;
    AdSelectedTopicHeaderLayout mTopicHeaderLayout;
    TopicListAdapter mAdapter;
    List<BookInfo> mBookInfos = new ArrayList<>();
    SelectedRequestParams mRequestParams;
    BookCityPresenter mBookCityPresenter;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_find_list_layout, null);
        unbinder = ButterKnife.bind(this, view);
        init();
        return view;
    }

    private void init() {
        mTitle.goneLeftContainer();
        mTitle.setTitle("发现");
        int statusBarHeight = DisplayUtils.getStatusBarHeight(getActivity());
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mTitle.getLayoutParams();
        params.topMargin = statusBarHeight;
        mTitle.setLayoutParams(params);
//        mSwipeRefresh.setPureScrollModeOn();
        mBookCityPresenter = new BookCityPresenter(lifeCyclerSubject);
        mBookCityPresenter.setMvpView(this);
        mAdapter = new TopicListAdapter(getActivity(), mBookInfos);
        mTopicHeaderLayout = new AdSelectedTopicHeaderLayout(getActivity(),true);
        mAdapter.addHeaderView(mTopicHeaderLayout);
        RecyclerViewHelper.initRecyclerViewV(getActivity(), mRecylerView, mAdapter);
        mRequestParams = new SelectedRequestParams();
        mRequestParams.type_name = Constants.BookCityModuleType.TYPE_FIND_LIST;
        mRequestParams.num = 20;
        mTopicHeaderLayout.refresh(Constants.BookCityModuleType.TYPE_FIND_BANNER, "优质书单", "");
        mBookCityPresenter.getTopics(mRequestParams);
        mAdapter.setOnItemClickListener((view, position) -> {
            position = position - mAdapter.getHeaderViewsCount();
            Map<String, String> map = new HashMap<>();
            map.put("title", mBookInfos.get(position).title);
            map.put("id", mBookInfos.get(position).id);
            ARouterUtils.toActivity(ARouterConfig.BOOK_MENU_DETAIL, map);
            AdPresenter.pvuvStaticsByString(getResources().getString(R.string.FXTUIJIAN));
        });
        mEmptyLayout.setRetryListener(() -> {
            mTopicHeaderLayout.refresh(Constants.BookCityModuleType.TYPE_FIND_BANNER, "优质书单", "");
            mBookCityPresenter.getTopics(mRequestParams);
        });
        SwipeRefreshHelper.init(mSwipeRefresh, () -> {
            mRequestParams.num = 20;
            mBookCityPresenter.getTopics(mRequestParams);
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void showLoading(String s) {

    }

    @Override
    public void dismissLoading() {
        if (mEmptyLayout != null) {
            mEmptyLayout.hide();
        }
    }

    @Override
    public void showContent(BaseResult<List<SelectedBean>> result) {
        DisplayUtils.visible(mRecylerView);
        dismissLoading();
        if (Utils.isEmptyList(result.result.data)) {
            if (mEmptyLayout != null) {
                mEmptyLayout.setEmptyStatus(EmptyLayout.STATUS_NO_DATA);
            }
            DisplayUtils.gone(mRecylerView);
            return;
        }
        mBookInfos.clear();
        mBookInfos.addAll(result.result.data.get(0).list);
        mAdapter.notifyDataSetChanged();
        SwipeRefreshHelper.swipeRefreshCompleted(mSwipeRefresh, mAdapter);
    }

    @Override
    public void showError(int errCode, String errDesc) {
        dismissLoading();
        SwipeRefreshHelper.swipeRefreshCompleted(mSwipeRefresh, mAdapter);
        if (!NetUtils.isNetworkAvailable(getActivity())) {
            int status = EmptyLayout.STATUS_NO_NET;
            if (mEmptyLayout != null) {
                mEmptyLayout.setEmptyStatus(status);
            }
            DisplayUtils.gone(mRecylerView);
        }
    }

    @Override
    public void switchTab(int tab) {

    }
}
